<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>InSTEDD | Outbox</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/forms-client.js"></script>
    <script type="text/javascript" src="js/survey.js"></script>
</head>
<body id="body" onload="LoadSurveys()">
    <script type="text/javascript">
        var fileSystemObject = top.fileSystemObject;
        var sentFolder = getSentFolder(fileSystemObject);
        var outboxFolder = getOutboxFolder(fileSystemObject);

        function LoadSurveys()
        {
            document.getElementById('errors').innerHTML = '';
            document.getElementById('progress').innerHTML = '';
            document.getElementById('filesDiv').innerHTML = '';
            ExecuteForEach(outboxFolder.Files, DisplaySurveyFile);
            
            if(outboxFolder.Files.Count == 0)
            {
                document.getElementById('sendAllButton').disabled = 'disabled';
            }
            else
            {
                document.getElementById('sendAllButton').disabled = '';
            }
        }
        
        function SendAllSurveys()
        {
            ExecuteForEach(outboxFolder.Files, SendSurvey);               
            top.RefreshOuboxAndSentLabels();
            document.getElementById('sendAllButton').disabled = 'disabled';
        }
                
        function DisplaySurveyFile(surveyPath)
        {
            var survey = new CreateSurveByFilePath(surveyPath);            

            var surveyFile = fileSystemObject.GetFile(surveyPath);  
            var surveyEditUrl = 'Survey.htm?surveyFile=' + surveyFile.Name + '&surveyFolder=outbox';         
            document.getElementById('filesDiv').innerHTML = 
                    document.getElementById('filesDiv').innerHTML + 
                    '<a class="surveyListItemlink" href="' +  surveyEditUrl + '">' + survey.getId() + '</a>' + 
                    '&nbsp&nbsp<span class="surveyListItem">' + survey.getFirstName() + '&nbsp' + survey.getLastName() +
                    '</span><br/>';            
        }        
        
        function SendSurvey(surveyFile)
        {    
            var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            var surveyStream = fileSystemObject.OpenTextFile(surveyFile,1,false,0);
            var surveyContent = surveyStream.ReadAll();
            surveyStream.Close();
        
            //xmlhttp.onreadystatechange=SendSurveyStateChanged;
            xmlhttp.open('GET', getSurveyServerUrl(), false);
            xmlhttp.send(surveyContent);
            
            if (xmlhttp.status == 200)
            {
                document.getElementById('progress').innerHTML = document.getElementById('progress').innerHTML + '<br/>The survey ' + surveyFile + ' was sent.'; 
                fileSystemObject.MoveFile(surveyFile, sentFolder + '\\' + surveyFile.Name);
            }
            else if(xmlhttp.status == 888)
            {
                var errorDescription = xmlhttp.responseText;
                document.getElementById('errors').innerHTML = errorDescription;
            }
            else
            {
                var survey = CreateSurveByFile(surveyFile);
                var errorDescription = xmlhttp.responseText;
                document.getElementById('errors').innerHTML = errorDescription;
                //document.getElementById('errors').innerHTML = 'The survey ' + survey.getId() + ' could not be sent and will not be moved from the outbox. Error: ' + xmlhttp.status;
            }
        }
        
//        function SendSurveyStateChanged()
//        {
//           if (xmlhttp.readyState == 4)
//           {
//              if (xmlhttp.status == 200)
//              {
//                    var surveyToBeMovedFileName = xmlhttp.responseText;
//                    fileSystemObject.MoveFile(outboxFolder + '\\' + surveyToBeMovedFileName, sentFolder + '\\' + surveyToBeMovedFileName);
//              }
//              else
//              {
//                    document.getElementById('errors').innerHTML = 'Error ' + xmlhttp.status + '. One or more surveys could not be sent. They will not be removed from the outbox.';            
//              }
//           }         
//        }
    </script>
    <div>
        <div class="sectionCaption">Forms to be sent</div>
        <div class="tip">(They still can be edited)</div><br />
        <div id="filesDiv">
        </div>
        <br /><input type="button" value="Send All" id="sendAllButton" onclick="SendAllSurveys()" /><br />
        <div style="color: Red;" id="errors">
        </div>
        <div style="color: Green;" id="progress">
        </div>    
    </div>
        
</body>
</html>
